import json
from typing import Dict, List

class JavaCodeGenerator:
    def __init__(self):
        self.indent_level = 0
        self.imports = set()

    def generate(self, ast: Dict) -> str:
        self.imports = set(ast.get("imports", []))
        lines = []

        # Package declaration
        if ast.get("package"):
            lines.append(f"package {ast['package']};")
            lines.append("")

        # Imports
        for imp in sorted(self.imports):
            lines.append(f"import {imp};")
        if self.imports:
            lines.append("")

        # Types
        for type_decl in ast["types"]:
            lines.extend(self._generate_type(type_decl))

        return "\n".join(lines)

    def _generate_type(self, type_decl: Dict) -> List[str]:
        if type_decl["type"] == "ClassDeclaration":
            return self._generate_class(type_decl)
        # Handle other types...

    def _generate_class(self, class_decl: Dict) -> List[str]:
        lines = []

        # Modifiers
        modifiers = " ".join(class_decl.get("modifiers", []))
        if modifiers:
            modifiers += " "

        # Class header
        extends = f" extends {class_decl['extends']}" if class_decl.get("extends") else ""
        implements = ""
        if class_decl.get("implements"):
            implements = " implements " + ", ".join(class_decl["implements"])

        lines.append(f"{modifiers}class {class_decl['name']}{extends}{implements} {{")
        self.indent_level += 1

        # Members
        for member in class_decl.get("members", []):
            if member["type"] == "FieldDeclaration":
                lines.extend(self._generate_field(member))
            elif member["type"] == "MethodDeclaration":
                lines.extend(self._generate_method(member))
            # Handle other member types...

        self.indent_level -= 1
        lines.append("}")

        return lines

    # Other generation methods...

if __name__ == "__main__":
    with open("sample_class_ast.json") as f:
        ast = json.load(f)

    generator = JavaCodeGenerator()
    java_code = generator.generate(ast)

    with open("EmployeeService.java", "w") as f:
        f.write(java_code)